package com.luoran.car.service.Impl;




import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luoran.car.domain.Coupon;

import com.luoran.car.mapper.CouponMapper;
import com.luoran.car.service.CouponService;

import com.luoran.car.service.UserCouponService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CouponServiceImpl  extends ServiceImpl<CouponMapper, Coupon>
        implements CouponService{

    @Autowired

   private CouponMapper couponMapper;
    @Autowired
    private UserCouponService userCouponService;


    @Override
    public List<Coupon> lists(Coupon coupon) {
        return couponMapper.lists(coupon);
    }

    @Override
    public List<Coupon> newList() {
        return couponMapper.newList();
    }

    @Override
    public boolean add(Coupon coupon) {
        return couponMapper.add(coupon);
    }


    @Override
    public Coupon getById(Integer id) {
        return couponMapper.getById(id);
    }

    @Override
    public boolean getCoupen(Integer couponId, Integer customerId) {
        Coupon coupon = getById(couponId);
        if(null==coupon ||coupon.getQuantity()<=0){
            return false;
        }
        coupon.setQuantity(coupon.getQuantity()-1);
        boolean b = updateById(coupon);
        if(b){
            boolean b1=userCouponService.addUserCoupon(coupon,customerId);
            if(b1){
                return true;
            }else {
               throw new RuntimeException("优惠券关系添加失败");
            }
        }else {
            throw new RuntimeException("优惠券更新失败");
        }

    }

}
